﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using MapCenterSystem.DataInfo;
using MapCenterSytem.Process;

using System.Threading;
using System.Resources;
using System.Globalization;

namespace MobiMap.Admin
{
    public partial class LogInUC : System.Web.UI.UserControl
    {       
        public string Username
        {
            get { return txtUsername.Text; }
            set { txtUsername.Text = value; }
        }
        public string Password
        {
            get { return txtPassword.Text; }
            set { txtPassword.Attributes.Add("value", value); }
        }
        public bool AccountVisible
        {
            get { return divAccount.Visible; }
            set { divAccount.Visible = value; }
        }
        public bool InputVisible
        {
            get { return divInput.Visible; }
            set { divInput.Visible = value; }
        }
        public string LogInError
        {
            get { return divError.InnerHtml; }
            set { divError.InnerHtml = value; }
        }

        ResourceManager rm;
        CultureInfo ci;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["mobimap_admininfo"] != null)
                {
                    this.Username = Request.Cookies["mobimap_admininfo"]["username"];
                    this.Password = Request.Cookies["mobimap_admininfo"]["userpass"];
                    chkRemember.Checked = true;
                }
                else
                {
                    this.Password = "123";
                }

                if (Session["mobimap_admin"] != null)
                {
                    if ((bool)Session["mobimap_admin"] == false)
                    {                        
                        divError.Visible = true;
                        //Response.Write("<script>alert('Tai khoan khong hop le! Vui long thu lai.');<script>");
                    }
                    else
                    {
                        InputVisible = false;
                        AccountVisible = true;

                        divError.Visible = true;
                    }
                }                            
            }

            //Language
            SetLanguage();
        }

        public void SetLanguage()
        {
            rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));            
            if (Request.Cookies["language"] != null)
            {
                string language = Request.Cookies["language"].Value;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
                LoadString(Thread.CurrentThread.CurrentCulture);
                                
                return;
            }

            //Defaut language is Vietnamese
            Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);            
        }

        public void LoadString(CultureInfo ci)
        {
            btnLogIn.Text = rm.GetString("Admin_LogInUC_SignIn", ci);
            linkSignOut.Text = rm.GetString("Admin_LogInUC_SignOut", ci);
            chkRemember.Text = rm.GetString("Admin_LogInUC_Remember", ci);
            divError.InnerHtml = rm.GetString("Admin_LogInUC_SignInError", ci);
            lbHello.Text = rm.GetString("Admin_LogInUC_HiAdmin", ci);            
        }

        protected void btnLogIn_Click(object sender, EventArgs e)
        {
            AdminDTO adminDTO = new AdminDTO(Username, Password);
            int result = AdminBUS.SignIn(adminDTO);
            if (result == 1)
            {
                Session["mobimap_admin"] = true;
                setCookie(Username, Password);
            }
            else
            {
                Session["mobimap_admin"] = false;
            }

            Response.Redirect(Request.Url.ToString());
        }

        protected void setCookie(string username, string password)
        {
            //HttpCookie myCookie = new HttpCookie("UserCookies");
            if (chkRemember.Checked)
            {
                Response.Cookies["mobimap_admininfo"]["username"] = username;
                Response.Cookies["mobimap_admininfo"]["userpass"] = password;
                Response.Cookies["mobimap_admininfo"].Expires = DateTime.Now.AddDays(30);
            }
            else
            {
                HttpCookie myCookie = new HttpCookie("mobimap_admininfo");
                myCookie.Expires = DateTime.Now.AddDays(-1d);
                Response.Cookies.Add(myCookie);
            }
        }

        protected void linkSignOut_Click(object sender, EventArgs e)
        {
            Session.Remove("mobimap_admin");
            Response.Redirect("ManageService.aspx");
        }
    }
}